package com.aisile.ssq.service.impl;


import com.aisile.model.ssq.pojos.Ssq;
import com.aisile.model.base.vos.R;
import com.aisile.model.ssq.vos.TreeResVo;
import com.aisile.ssq.mapper.SsqMapper;
import com.aisile.ssq.service.SsqService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther:yry
 * @Date:2024/6/20 0020
 * @VERSON:1.0
 */
@Service
public class SsqServiceImpl extends ServiceImpl<SsqMapper, Ssq> implements SsqService {
    @Override
    public R findSsqByParentId(Integer parentId) {
        if(parentId==null){
            return R.fail("查询错误");
        }
        List<Ssq> list =  this.baseMapper.selectList(
                Wrappers.<Ssq>lambdaQuery().eq(
                        Ssq::getParentId,parentId
                )
        );
        return R.ok(list);
    }

    /**
     * 0
     * @param id
     * @return
     */
    @Override
    public List<TreeResVo> dg(int id) {
        List<TreeResVo> treeResVos = new ArrayList<>();
        // 起始值(一级数据)
        LambdaQueryWrapper<Ssq> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Ssq::getParentId,id);
        List<Ssq> list =  this.baseMapper.selectList(lambdaQueryWrapper);
        for(Ssq item:list){
            // 调用dg
            TreeResVo treeResVo = new TreeResVo();
            treeResVo.setLabel(item.getName());
            treeResVo.setChildren(dg(item.getId()));
            treeResVos.add(treeResVo);
        }
        return treeResVos;
    }
}
